package com.ruoyi.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.web.domain.PaymentApplyResult;
import com.ruoyi.web.request.IESI123104Request;
import com.ruoyi.web.request.IESI123105Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.MerInfoMapper;
import com.ruoyi.web.domain.MerInfo;
import com.ruoyi.web.service.IMerInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class MerInfoServiceImpl implements IMerInfoService 
{
    @Autowired
    private MerInfoMapper merInfoMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询商户信息
     * 
     * @param merchCd 商户信息ID
     * @return 商户信息
     */
    @Override
    public MerInfo selectMerInfoById(String merchCd)
    {
        return merInfoMapper.selectMerInfoById(merchCd);
    }

    /**
     * 查询商户信息列表
     * 
     * @param merInfo 商户信息
     * @return 商户信息
     */
    @Override
    public List<MerInfo> selectMerInfoList(MerInfo merInfo)
    {
        return merInfoMapper.selectMerInfoList(merInfo);
    }

    /**
     * 新增商户信息
     * 
     * @param merInfo 商户信息
     * @return 结果
     */
    @Override
    public int insertMerInfo(MerInfo merInfo)  throws Exception
    {
        IESI123105Request request =new IESI123105Request();
        request.setAddInfo(merInfo.getAddInfo());
        HashMap<String, Object> resultMap= agentPayService.IESI123105(request,OrderIdGenerator.getDefault().nextId(),merInfo.getMerchCd());
        if (resultMap.get("balance")!=null){
            merInfo.setBalance(new BigDecimal((String)resultMap.get("balance")));
        }
        merInfo.setAccount((String) resultMap.get("account"));
        merInfo.setAccountName((String) resultMap.get("accountName"));
        merInfo.setCurrency((String) resultMap.get("currency"));
        merInfo.setCustNo((String) resultMap.get("custNo"));
        merInfo.setOpenBranchCode((String) resultMap.get("openBranchCode"));
        MerInfo old =selectMerInfoById(merInfo.getMerchCd());
        if (old!=null){
           return merInfoMapper.updateMerInfo(merInfo);
        }
        return merInfoMapper.insertMerInfo(merInfo);
    }

    /**
     * 修改商户信息
     * 
     * @param merInfo 商户信息
     * @return 结果
     */
    @Override
    public int updateMerInfo(MerInfo merInfo) throws Exception
    {
        IESI123105Request request =new IESI123105Request();
        request.setAddInfo(merInfo.getAddInfo());
        HashMap<String, Object> resultMap= agentPayService.IESI123105(request,OrderIdGenerator.getDefault().nextId(),merInfo.getMerchCd());
        if (resultMap.get("balance")!=null){
            merInfo.setBalance(new BigDecimal((String)resultMap.get("balance")));
        }
        merInfo.setAccount((String) resultMap.get("account"));
        merInfo.setAccountName((String) resultMap.get("accountName"));
        merInfo.setCurrency((String) resultMap.get("currency"));
        merInfo.setCustNo((String) resultMap.get("custNo"));
        merInfo.setOpenBranchCode((String) resultMap.get("openBranchCode"));
        MerInfo old =selectMerInfoById(merInfo.getMerchCd());
        if (old==null){
           return merInfoMapper.insertMerInfo(merInfo);
        }
        return merInfoMapper.updateMerInfo(merInfo);
    }

    /**
     * 删除商户信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerInfoByIds(String ids)
    {
        return merInfoMapper.deleteMerInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户信息信息
     * 
     * @param merchCd 商户信息ID
     * @return 结果
     */
    @Override
    public int deleteMerInfoById(String merchCd)
    {
        return merInfoMapper.deleteMerInfoById(merchCd);
    }
}
